package com.hotec.PROJECT_NAME.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.hotec.PROJECT_NAME.component.exception.exc.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Date;

@Slf4j
public class AliOSSUtil {
    private static String       END_POINT         = "oss-cn-beijing.aliyuncs.com";
    private static String       ACCESS_KEY_ID     = "LTAI4GJ1FFWQuJaGFLyuctCj";
    private static String       ACCESS_KEY_SECRET = "SH8KQFyEeWSX5mPdpOgqBxBd9txJUZ";
    private static String       BUCKET_NAME       = "shangchi";
    // 文件访问域名
    private static String       FILE_HOST         = "shangchi.oss-cn-beijing.aliyuncs.com";

    private static OSSClient getClient(){
        // 创建OSS客户端
        OSSClient ossClient = new OSSClient(END_POINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        // 判断文件容器是否存在，不存在则创建
        if (!ossClient.doesBucketExist(BUCKET_NAME)) {
            ossClient.createBucket(BUCKET_NAME);
            CreateBucketRequest createBucketRequest = new CreateBucketRequest(BUCKET_NAME);
            createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
            ossClient.createBucket(createBucketRequest);
        }
        return ossClient;
    }


    public static String upload(InputStream inputStream,String fileExt){
        // 创建文件路径
        String fileUrl = DateUtil.formatDate(new Date()) + "/" + IdUtil.fastUUID();
        if(StrUtil.isNotBlank(fileExt)){
            fileUrl=fileUrl+"."+fileExt;
        }
        if (inputStream == null) {
            return null;
        }
        OSSClient ossClient = getClient();
        try {
            // 上传文件
            PutObjectResult result = ossClient.putObject(new PutObjectRequest(BUCKET_NAME, fileUrl, inputStream));
            if (null != result) {
                return FILE_HOST + "/"+fileUrl;
            }
        } catch (OSSException oe) {
            log.error(oe.getMessage());
        } catch (ClientException ce) {
            log.error(ce.getMessage());
        } finally {
            // 关闭OSS服务，一定要关闭
            ossClient.shutdown();
        }
        return null;
    }


    /**
     * 方法描述:上传文件
     *
     * @author leon 2016年12月26日 下午3:33:13
     * @param inputStream 文件流
     * @return
     * @throws FileNotFoundException
     */
    public static String upload(InputStream inputStream){
        return upload(inputStream,null);
    }

    public static String upload(MultipartFile multipartFile){
        try {
            InputStream inputStream = multipartFile.getInputStream();
            return upload(inputStream,FilenameUtils.getExtension( multipartFile.getOriginalFilename() ));
        } catch (IOException e) {
            throw new BizException("上传异常!");
        }
    }

    /**
     * 方法描述:上传文件
     *
     * @author leon 2016年12月16日 上午11:40:34
     * @param file 文件对象
     * @return
     */
    public static String upload(File file) {
        if (file == null) {
            return null;
        }
        try {
            InputStream inputStream = new FileInputStream(file);
            return upload(inputStream,FilenameUtils.getExtension(file.getName()));
        } catch (FileNotFoundException e) {
            UtilsLog.log(AliOSSUtil.class, e);
            throw new BizException("上传异常!");
        }
    }

}
